home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
et
/
et3_0-a1.lha
/
et3
/
src
/
ProgressDialog.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-07-08
|
4KB
|
206 lines
#ifdef __GNUG__
#pragma implementation
#endif
#include "ProgressDialog.h"
#include "Class.h"
#include "Dialog.h"
#include "Expander.h"
#include "TextItem.h"
#include "WindowSystem.h"
#include "Window.h"
#include "WindowPort.h"
#include "Buttons.h"
#include "String.h"
#include "Look.h"
#include "BorderItems.h"
#include "Alert_e.h"
#include "CLib.h"
//---- AlarmHandler ------------------------------------------------------------
class AlarmHandler : public SysEvtHandler {
ProgressImpl *progress;
public:
MetaDef(AlarmHandler);
AlarmHandler(ProgressImpl *pi) : SysEvtHandler(eSigAlarm)
{ progress= pi; }
void Notify(SysEventCodes, int)
{ progress->timeout= TRUE; }
};
NewMetaImpl0(AlarmHandler, SysEvtHandler);
//---- ProgressBar -------------------------------------------------------------
class ProgressBar : public VObject {
public:
int val;
public:
ProgressBar() : VObject(Point(20))
{ }
Metric GetMinSize()
{ return gLook->ProgressBarLayout()->GetMinSize(this); }
void Draw(Rectangle r)
{ gLook->ProgressBarLayout()->Adorn(this, r, val); }
void SetVal(int v);
};
void ProgressBar::SetVal(int v)
{
val= v;
ForceRedraw();
if (GetWindow()) {
WindowPort *wp= (WindowPort*) GetWindow()->GetPortDesc();
wp->DevUpdate();
}
}
//---- ProgressDialog ----------------------------------------------------------
const int cIdStop = cIdFirstUser + 100;
class ProgressDialog : public Dialog {
ProgressBar *bar;
TextItem *ti;
public:
bool quit;
VObject *DoMakeContent();
public:
MetaDef(ProgressDialog);
ProgressDialog(ProgressBar *b, TextItem *t) : Dialog("Progress", eWinFixed)
{ bar= b; ti= t; }
void Control(int id, int part, void *val);
void Show();
};
NewMetaImpl0(ProgressDialog, Dialog);
void ProgressDialog::Show()
{
quit= FALSE;
bar->val= 0;
ShowAt(gWindow, gWindow->contentRect.Center());
}
VObject *ProgressDialog::DoMakeContent()
{
return
new Matte(
new VExpander(gPoint10,
ti,
bar,
new ActionButton(cIdStop, "Stop"),
0
)
);
}
void ProgressDialog::Control(int id, int part, void *val)
{
if (id == cIdStop)
quit= TRUE;
else
Dialog::Control(id, part, val);
}
//---- ProgressImpl ------------------------------------------------------------
ProgressImpl::ProgressImpl()
{
}
ProgressImpl::~ProgressImpl()
{
if (pb == 0)
SafeDelete(ti);
SafeDelete(pb);
}
void ProgressImpl::SetUp()
{
gWindowSystem->SetWait();
if (alarmhandler == 0)
gSystem->AddAsyncSignalHandler(alarmhandler= new AlarmHandler(this));
CLib::Alarm(1);
}
void ProgressImpl::SetMessage(char *message)
{
if (ti)
ti->SetString(message);
else
ti= new TextItem(message);
}
bool ProgressImpl::SetVal()
{
if ((pb == 0 || ! pb->IsOpen()) && curr > /* (max*2)/3 */ max) {
timeout= FALSE;
return FALSE;
}
if (bar == 0)
bar= new ProgressBar();
int v= curr*bar->contentRect.extent.x/max;
if (v != bar->val) {
if (pb == 0)
pb= new ProgressDialog(bar, ti);
if (! pb->IsOpen()) {
bar->SetVal(v);
pb->ShowOnWindow(gWindow);
} else
bar->SetVal(v);
((WindowPort*) pb->GetWindow()->GetPortDesc())->Poll(0);
if (pb->quit) {
pb->quit= FALSE;
if (ShowAlert(eAlertStop, "Abort %s ?", ti->AsString()) == cIdYes) {
Abort(FALSE);
return TRUE;
}
}
}
return FALSE;
}
void ProgressImpl::Abort(bool skiptoend)
{
gWindowSystem->ResetWait();
CLib::Alarm(0);
if (pb && pb->IsOpen()) {
if (skiptoend)
for (int i= bar->val; i < bar->contentRect.extent.x; i++)
bar->SetVal(i);
pb->Close();
}
}
//---- ProgressFilter ----------------------------------------------------------
ProgressFilter::ProgressFilter() : StreamBuf()
{
}
int ProgressFilter::Underflow(u_char*, int size)
{
size= sb->salloc(&base, BUFSIZE);
if (size == EOF)
return EOF;
if (gProgress->Inc(size))
return EOF;
return size;
}
int ProgressFilter::Overflow(u_char *bp, int size)
{
if (gProgress->Inc(size))
return EOF;
return size= sb->sputn((char*)bp, size);
}